<template>
  <div class="home-slide">
    <el-scrollbar class="slide-content" ref="slideScrollView" id="slide-content">
      <div class="slide-menu" id="slide-menu">
        <div
          :class="{
            'menu-item': true,
            'guide-step': [
              'JXGL_ORGAN_MANAGE',
              'JXGL_INDEX_MANAGE',
              'JXGL_MOULD_MANAGE',
              'JXGL_ASSESS_MANAGE',
            ].includes(itm1.belongPage),
          }"
          :id="getIdStr(itm1.belongPage)"
          v-for="itm1 in getMenu(leftNavMenus)"
          :key="itm1.belongPage">
          <!-- actived: pMenu === itm1.belongPage, -->
          <div
            class="menu-item-title"
            :class="{
              actived: getActive(itm1.belongPage),
              'menu-item-title-menu': (!itm1.menuVOS && itm1.menuName) || (itm1.menuVOS && itm1.menuVOS.length === 0),
            }"
            @click.stop="toRoutePage(itm1.belongPage, itm1.belongPage, itm1.menuVOS || [])">
            <i :class="itm1.menuImagePath"></i>
            <span>{{ itm1.menuName }}</span>
            <span
              v-if="
                ['JXGL_ORGAN_MANAGE', 'JXGL_INDEX_MANAGE', 'JXGL_MOULD_MANAGE', 'JXGL_ASSESS_MANAGE'].includes(
                  itm1.belongPage
                )
              "
              :class="{
                mark: true,
                'scheme-mark': itm1.belongPage === 'JXGL_ASSESS_MANAGE',
                'model-mark': itm1.belongPage === 'JXGL_INDEX_MANAGE',
                'org-mark': itm1.belongPage === 'JXGL_MOULD_MANAGE',
                'system-mark': itm1.belongPage === 'JXGL_ORGAN_MANAGE',
              }">
              <span>{{ itm1.belongPage | markName }}</span>
            </span>
          </div>
          <ul class="menu-item-list" v-if="itm1.menuVOS && itm1.menuVOS.length">
            <li
              :class="[
                'menu-item-li',
                activeName === itm2.belongPage ? 'active' : '',
                ['JXGL_CONSUMER_MANAGE', 'JXGL_ROLE_MANAGE', 'JXGL_POST_MANAGE'].includes(itm2.belongPage)
                  ? 'guide-step'
                  : '',
              ]"
              v-for="itm2 in itm1.menuVOS"
              :key="itm2.belongPage"
              @click.stop="toRoutePage(itm2.belongPage, itm1.belongPage, [])"
              :id="getIdStr(itm2.belongPage)">
              {{ itm2.menuName }}
            </li>
          </ul>
        </div>
      </div>
    </el-scrollbar>
    <div class="other-set" v-if="leftMenuPcode === 'JXKH_JXGL'">
      <div class="set-title">其他设置</div>
      <div
        class="set-item"
        :class="[activeName === item.belongPage ? 'active' : '']"
        @click.stop="setGo(item.belongPage)"
        v-for="(item, index) in otherMenus"
        :key="index">
        <img :src="activeName === item.belongPage ? item.menuImagePathAct : item.menuImagePath" alt="" />
        <span>{{ item.menuName }}</span>
      </div>
    </div>
    <div class="version-info">
      <version-label></version-label>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { findChildItem } from '@utility/utility'

import VersionLabel from '../../version/VersionLabel.vue'

export default {
  name: 'HomeSlide',
  components: { VersionLabel },
  data() {
    return {
      activeName: '',
      pMenu: '',
      otherMenus: [],
    }
  },
  computed: {
    ...mapGetters(['leftNavMenus', 'leftMenuPcode']),
  },
  watch: {
    // leftNavMenus: {
    //   deep: true,
    //   handler() {
    //     this.goFirstPage()
    //   },
    // },
    '$route.name'(val) {
      if (val !== 'layoutSlide') {
        this.setMenuActive()
      }
    },
  },
  filters: {
    markName(key) {
      const markInfo = {
        JXGL_ORGAN_MANAGE: '先添加',
        JXGL_INDEX_MANAGE: '建指标',
        JXGL_MOULD_MANAGE: '再配置',
        JXGL_ASSESS_MANAGE: '后发起',
      }
      return markInfo[key]
    },
  },
  created() {
    // if (this.$route.name === 'layoutSlide') {
    //   this.goFirstPage()
    // } else {
    const { group } = this.$route.meta
    if (group) {
      this.$store.commit('setLeftMenuPcode', group)
    }
    this.setMenuActive()

    this.$EventBus.$on('scroll', top => {
      this.$refs.slideScrollView.wrap.scrollTop = top
    })
    // }
  },
  methods: {
    setMenuActive() {
      this.activeName = this.$route.name
      // console.log('%c⧭', 'color: #731d1d', this.activeName)
      const { parent } =
        findChildItem(this.leftNavMenus, this.activeName, {
          fieldId: 'belongPage',
          fieldChild: 'menuVOS',
        }) || {}

      if (parent) {
        this.pMenu = parent.belongPage
      } else {
        this.pMenu = this.activeName
      }
      if (this.leftMenuPcode === 'JXKH_JXGL') {
        const arr = this.leftNavMenus.slice(-1)
        // console.log('arr :>> ', arr)
        this.otherMenus = arr[0].menuVOS
      } else {
        this.otherMenus = []
      }
      // console.log('this.otherMenus :>> ', this.otherMenus)
    },
    // async goFirstPage() {
    //   const firstPathName = this.findMenuPathName(this.leftNavMenus)
    //   if (firstPathName && firstPathName !== this.$route.name) {
    //     this.$router.replace({ name: firstPathName })
    //   }
    // },
    findMenuPathName(arr) {
      if (arr?.length) {
        if (arr[0].menuVOS?.length) {
          return this.findMenuPathName(arr[0].menuVOS)
        } else {
          return arr[0].name
        }
      }
      return null
    },
    getIdStr(code) {
      const idStr = {
        JXGL_ORGAN_MANAGE: 'guide-step-1',
        JXGL_CONSUMER_MANAGE: 'guide-step-2',
        JXGL_ROLE_MANAGE: 'guide-step-3',
        JXGL_POST_MANAGE: 'guide-step-4',
        JXGL_INDEX_MANAGE: 'guide-step-5',
        JXGL_MOULD_MANAGE: 'guide-step-6',
        JXGL_ASSESS_MANAGE: 'guide-step-7',
      }
      return idStr[code]
    },
    toRoutePage(name, pMenu, children) {
      if (name === this.activeName) {
        return false
      }
      if (children.length === 0) {
        this.activeName = name
        this.pMenu = pMenu
        this.$router.push({ name: name })
      }
    },
    setGo(name) {
      if (name === this.activeName) {
        return false
      }
      this.activeName = name
      this.pMenu = 'JXGL_OTHER_MANAGE'
      this.$router.push({ name: name })
    },
    getMenu(list) {
      if (this.leftMenuPcode === 'JXKH_JXGL') {
        return list.slice(0, -1)
      } else if (this.leftMenuPcode === 'JXKH_PSKH') {
        return list.slice(0, -2)
      } else {
        return list
      }
    },
    getActive(belongPage) {
      if (this.pMenu === 'PSKH_SYSTEM_MESSAGE') {
        return false
      }
      if (this.pMenu === 'PSKH_SYSTEM_FILE_ADD' || this.pMenu === 'PSKH_SYSTEM_FILE_DETAIL') {
        if (this.$route.params.type === '0' && belongPage === 'PSKH_WORK_NOTICE') return true
        if (this.$route.params.type === '4' && belongPage === 'PSKH_SYSTEM_FILE') return true
        return false
      } else {
        const idx = this.pMenu.lastIndexOf('_')
        const str = this.pMenu.slice(0, idx)
        if (belongPage.indexOf(str) !== -1) {
          return true
        } else {
          return false
        }
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.home-slide {
  width: 100%;
  height: 100%;
  background: #f3f9ff;
  background-image: url('../../assets/images/home/slide-nav-bgi.png');
  background-repeat: no-repeat;
  background-size: 100% auto;
  background-position: left bottom;
  display: flex;
  flex-direction: column;

  .slide-content {
    flex: 1;
    overflow: hidden;
    padding-bottom: 20px;
    border-bottom: 1px solid #e4eaf0;
  }

  .slide-menu {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-top: 20px;
    padding: 15px 0 15px 20px;

    .menu-item {
      width: 100%;
      position: relative;
      overflow: hidden;

      &-title {
        font-size: 16px;
        color: #666;
        line-height: 60px;
        // margin-bottom: 10px;
        position: relative;
        &.actived {
          i {
            color: #2b78f1;
          }
        }

        i {
          font-size: 24px;
          font-weight: normal;
          vertical-align: middle;
          color: #8992ae;
          margin: 0 16px;
        }
        .mark {
          position: relative;
          bottom: 0;
          font-size: 12px;
          line-height: 1;
          padding: 3px 6px;
          border-bottom-right-radius: 11px;
          border-top-left-radius: 11px;
          // font-weight: 600;
          margin-left: 14px;
        }
        .mark.scheme-mark {
          color: #00bf6f;
          background: rgba(0, 191, 111, 0.1);
        }
        .mark.org-mark {
          color: #e6a23c;
          background: #fdf6ec;
        }
        .mark.model-mark {
          color: #2b78f1;
          background: rgba(38, 114, 255, 0.1);
        }
        .mark.system-mark {
          color: #ff6d56;
          background: rgba(255, 109, 86, 0.1);
        }
      }

      &-title-menu {
        cursor: pointer;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
      }

      &-title-menu.actived {
        background-color: rgba(0, 84, 196, 0.08);
        color: #2b78f1;
        border-right: 4px solid #2b78f1;
      }

      &-li {
        padding-left: 85px;
        font-size: 14px;
        line-height: 60px;
        // margin-bottom: 10px;
        cursor: pointer;
        color: #666;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
        position: relative;
      }

      &-li.active {
        background-color: rgba(0, 84, 196, 0.08);
        color: #2b78f1;
        border-right: 4px solid #2b78f1;
      }
    }
  }

  .other-set {
    padding: 15px 30px;
    font-size: 14px;
    line-height: 35px;
    .set-item {
      display: flex;
      align-items: center;
      cursor: pointer;
      padding: 0 20px;
      img {
        margin-right: 9px;
        width: 15px;
        height: 15px;
      }
    }
    .active {
      background-color: rgba(0, 84, 196, 0.08);
      color: #2b78f1;
    }
  }

  .version-info {
    text-align: center;
    padding: 10px;
  }
}
</style>
